# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

package dcExplorer::wxui::widget::Units;
use strict;
use utf8;
use Wx;
use base 'Wx::ScrolledWindow';
use wxui::widget::Unit;
use Wx qw(wxHORIZONTAL wxVERTICAL wxEXPAND wxALL);

sub new
{
	my $proto = shift;
	my $parent = shift;
	my $id = shift;
	my $class = ref($proto) || $proto;
	my $self = $class->SUPER::new($parent, $id);
	$self->SetBackgroundColour(Wx::Colour->new(0, 0, 0));
	$self->SetScrollRate(10, 10);
	$self->{Units} = shift;
	$self->{Sizer} = Wx::BoxSizer->new(wxVERTICAL);
	for my $unit ( 
		sort {
			defined $a->getProp('is_transportable') && defined $b->getProp('is_transportable') ? 
				$a->getProp('is_transportable') <=> $b->getProp('is_transportable') : 
				0 ||
			defined $a->getProp('weight') && defined $b->getProp('weight') ? -($a->getProp('weight') <=> $b->getProp('weight')) : 0 || 
			defined $a->getProp('name') && defined $b->getProp('name') ? $a->getProp('name') cmp $b->getProp('name') : 0
		} 
		@{$self->{Units}}
	)
	{
		my $unitWidget = dcExplorer::wxui::widget::Unit->new($self, -1, $unit);
		$self->{Sizer}->Add($unitWidget, 0, wxEXPAND, wxALL, 0);
	}
	$self->SetSizer($self->{Sizer});
	bless($self, $class);
	return $self;
}

1;
